<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2014
 * ---
 * Repraesentiert ein Spiel
 * Methoden, um Spiele aus der DB abzurufen, editieren und loeschen
 *
 * @author Lopo
 */
class Game {
    private $id = null;
    private $hometeam = null;
    private $awayteam = null;
    private $date = null;
    private $type= null;
    private $imagelink = null;
    private $reportlink = null;
    private $goalshome = null;
    private $goalsaway = null;
    private $team = null;
    private $season = null;
    private $venue = null;


    public function __construct($data = array()) {    
        if (isset($data['game_id'])) $this->id = (int) $data['game_id'];
        if (isset($data['game_hometeam'])) $this->hometeam = $data['game_hometeam'];
        if (isset($data['game_awayteam'])) $this->awayteam = $data['game_awayteam'];
        if (isset($data['game_date'])) $this->date = $data['game_date'];
        if (isset($data['game_type'])) $this->type= (int) $data['game_type'];
        if (isset($data['game_imagelink'])) $this->imagelink= $data['game_imagelink'];
        if (isset($data['game_reportlink'])) $this->reportlink= $data['game_reportlink'];
        if (isset($data['game_goalshome'])) $this->goalshome= (int) $data['game_goalshome'];
        if (isset($data['game_goalsaway'])) $this->goalsaway= (int) $data['game_goalsaway'];
        if (isset($data['game_team'])) $this->team = (int) $data['game_team'];
        if (isset($data['game_season'])) $this->season= (int) $data['game_season'];
        if (isset($data['game_venue'])) $this->venue= (int) $data['game_venue'];
    }
	
	/**
     * 
     * @param type $id
     * @return \Game
     */
	public static function getGameById($id) {
        $db = new Database();

        $sql = "SELECT * FROM ff_games WHERE game_id = :id";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $id, PDO::PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch();
        
        $db = null;

        if ($row) return new Game($row);
    }
	
	public static function getGamesByTeamAndSeason($team_id, $season_id) {
		$db = new Database();
		
		$sql = "SELECT * FROM ff_games WHERE game_team = :team_id AND game_season = :season_id";
		
		$stmt = $db->prepare($sql);
        $stmt->bindValue(":team_id", $team_id, PDO::PARAM_INT);
        $stmt->bindValue( ":season_id", $season_id, PDO::PARAM_INT );
        $stmt->execute();
	
		$list = array();
        while ($row = $stmt->fetch()) {
            $game = new Game($row);
            $list[] = $game;
        }

        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;
        
        return (array("gameList" => $list, "totalRows" => $totalRows[0]));
	}
	
	public static function getGamesByTeam($team_id) {
		$db = new Database();
		
		$sql = "SELECT * FROM ff_games WHERE game_team = :team_id";
		
		$stmt = $db->prepare($sql);
        $stmt->bindValue(":team_id", $team_id, PDO::PARAM_INT);
        $stmt->execute();
	
		$list = array();
        while ($row = $stmt->fetch()) {
            $game = new Game($row);
            $list[] = $game;
        }

        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;
        
        return (array("gameList" => $list, "totalRows" => $totalRows[0]));
	}
	
	public static function getGamesBySeason($season_id) {
		$db = new Database();
		
		$sql = "SELECT * FROM ff_games WHERE game_season = :season_id";
		
		$stmt = $db->prepare($sql);
                $stmt->bindValue( ":season_id", $season_id, PDO::PARAM_INT );
                $stmt->execute();
	
		$list = array();
        while ($row = $stmt->fetch()) {
            $game = new Game($row);
            $list[] = $game;
        }

        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;
        
        return (array("gameList" => $list, "totalRows" => $totalRows[0]));
	}
	
	public static function getGames() {
		$db = new Database();
		
		$sql = "SELECT * FROM ff_games";
		
		$stmt = $db->prepare($sql);
        $stmt->execute();
	
		$list = array();
        while ($row = $stmt->fetch()) {
            $game = new Game($row);
            $list[] = $game;
        }

        $sql = "SELECT FOUND_ROWS() AS totalRows";
        $totalRows = $db->query($sql)->fetch();
        
        $db = null;
        
        return (array("gameList" => $list, "totalRows" => $totalRows[0]));
	}
	
	public static function getNextGameByTeam($team_id) {
		$db = new Database();

        $sql = "SELECT * FROM ff_games WHERE game_team = :team_id AND DATE(game_date) >= CURDATE() ORDER BY game_date LIMIT 1";

        $stmt = $db->prepare($sql);
        $stmt->bindValue(":team_id", $team_id, PDO::PARAM_INT);
        $stmt->execute();
        $row = $stmt->fetch();
        
        $db = null;

        if ($row) return new Game($row);
	}
	
        
        public function setScorers($scorerIds) {
		$db = new Database();
		
		$sqlDelete = "DELETE FROM ff_scorers WHERE game_id = :gameId";
                $stmt = $db->prepare($sqlDelete);
		$stmt->bindValue(":gameId", $this->getId(), PDO::PARAM_INT);
		$stmt->execute();
		
		foreach ($scorerIds as $scorerId) {
			$sqlInsert = "INSERT INTO ff_scorers (game_id, player_id) VALUES (:game_id, :player_id)";
			$stmt = $db->prepare($sqlInsert);
			$stmt->bindValue(":game_id", $this->id, PDO::PARAM_INT);
			$stmt->bindValue(":player_id", $scorerId, PDO::PARAM_INT);
			$stmt->execute();
		}
		$db = null;
	}
        
	public function insert() {
        if ($this->id != 0) {
            trigger_error("Game::insert(): Es wird versucht ein neues Spiel anzulegen, obwohl bereits die ID gesetzt wurde (ID: $this->id).", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "INSERT INTO ff_games (game_hometeam, game_awayteam, game_date, game_type, game_imagelink, game_reportlink, game_goalshome, game_goalsaway, game_team, game_season, game_venue) VALUES (:hometeam, :awayteam, :date, :type, :imagelink, :reportlink, :goalshome, :goalsaway, :team, :season, :venue)";
		
		$stmt = $db->prepare($sql);
        $stmt->bindValue(":hometeam", $this->hometeam, PDO::PARAM_STR);
        $stmt->bindValue(":awayteam", $this->awayteam, PDO::PARAM_STR);
        $stmt->bindValue(":date", $this->date, PDO::PARAM_STR);
        $stmt->bindValue(":type", $this->type, PDO::PARAM_INT);
        $stmt->bindValue(":imagelink", $this->imagelink, PDO::PARAM_STR);
        $stmt->bindValue(":reportlink", $this->reportlink, PDO::PARAM_STR);
        $stmt->bindValue(":goalshome", $this->goalshome, PDO::PARAM_INT);
        $stmt->bindValue(":goalsaway", $this->goalsaway, PDO::PARAM_INT);
        $stmt->bindValue(":team", $this->team, PDO::PARAM_INT);
        $stmt->bindValue(":season", $this->season, PDO::PARAM_INT);
        $stmt->bindValue(":venue", $this->venue, PDO::PARAM_INT);
        $stmt->execute();
        
        $this->id = $db->lastInsertId();
        
        $db = null;
    }
	
	public function update() {
        if (is_null($this->id)) {
            trigger_error("Game::update(): Es wird versucht ein Spiel zu speichern, das keine ID hat - was nicht geht beim Update.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "UPDATE ff_games SET game_hometeam=:hometeam, game_awayteam=:awayteam, game_date=:date, game_type=:type, game_imagelink=:imagelink, game_reportlink=:reportlink, game_goalshome=:goalshome, game_goalsaway=:goalsaway, game_team=:team, game_season=:season, game_venue=:venue WHERE game_id=:id";
		
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":hometeam", $this->hometeam, PDO::PARAM_STR);
        $stmt->bindValue(":awayteam", $this->awayteam, PDO::PARAM_STR);
        $stmt->bindValue(":date", $this->date, PDO::PARAM_STR);
        $stmt->bindValue(":type", $this->type, PDO::PARAM_INT);
        $stmt->bindValue(":imagelink", $this->imagelink, PDO::PARAM_STR);
        $stmt->bindValue(":reportlink", $this->reportlink, PDO::PARAM_STR);
        $stmt->bindValue(":goalshome", $this->goalshome, PDO::PARAM_INT);
        $stmt->bindValue(":goalsaway", $this->goalsaway, PDO::PARAM_INT);
        $stmt->bindValue(":team", $this->team, PDO::PARAM_INT);
        $stmt->bindValue(":season", $this->season, PDO::PARAM_INT);
        $stmt->bindValue(":venue", $this->venue, PDO::PARAM_INT);
        $stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
	
	public function delete() {
        if (is_null($this->id)) {
            trigger_error("Game::delete(): Es wird versucht ein Spiel zu loeschen, das keine ID hat - was nicht geht beim Loeschen.", E_USER_ERROR);
        }

        $db = new Database();

        $sql = "DELETE FROM ff_games WHERE game_id = :id LIMIT 1";
        
        $stmt = $db->prepare($sql);
        $stmt->bindValue(":id", $this->id, PDO::PARAM_INT);
        $stmt->execute();
        
        $db = null;
    }
    
    public function getHometeam()
    {
        return $this->hometeam;
    }
    public function getId() {
        return $this->id;
    }

    public function getAwayteam() {
        return $this->awayteam;
    }

    public function getDate() {
        return $this->date;
    }

    public function getType() {
        return $this->type;
    }

    public function getImagelink() {
        return $this->imagelink;
    }

    public function getReportlink() {
        return $this->reportlink;
    }

    public function getGoalshome() {
        return $this->goalshome;
    }

    public function getGoalsaway() {
        return $this->goalsaway;
    }

    public function getTeam() {
        return $this->team;
    }

    public function getSeason() {
        return $this->season;
    }

    public function getVenue() {
        return $this->venue;
    }


}
?>
